package com.levine.base_libs.utils;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import com.blankj.utilcode.util.SizeUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.levine.base_libs.view.RoundCornersTransformation;

public class GlideUtil {

    /**
     * @param activity
     * @param imagePath         图片路径，包括url，path，resource
     * @param roundingRadiusDip 圆角
     * @param isCenterCrop      是否居中裁剪
     * @param isCrossFade       是否缓冲显示
     * @param imageView         需要显示的view
     */
    public static void loadImage(Activity activity,
                                 Object imagePath,
                                 int roundingRadiusDip,
                                 boolean isCenterCrop,
                                 boolean isCrossFade,
                                 ImageView imageView) {
        RequestBuilder<Drawable> requestBuilder = Glide.with(activity).load(imagePath);
        if (isCenterCrop) {
            requestBuilder = requestBuilder.apply(new RequestOptions().transforms(new CenterCrop(),
                    new RoundedCorners(SizeUtils.dp2px(roundingRadiusDip))));
        } else {
            requestBuilder = requestBuilder.apply(new RequestOptions().transforms(new RoundedCorners(SizeUtils.dp2px(roundingRadiusDip))));
        }
        if (isCrossFade) {
            requestBuilder = requestBuilder.transition(new DrawableTransitionOptions().crossFade());
        }
        requestBuilder.into(imageView);

    }

    /*加载圆形图*/
    public static void loadCircleImage(Activity activity,
                                       Object imagePath,
                                       ImageView imageView) {
        Glide.with(activity).load(imagePath)
                .apply(new RequestOptions().circleCrop())
                .transition(new DrawableTransitionOptions().crossFade())
                .into(imageView);
    }

    /*加载圆角图*/
    public static void loadRoundImage(Activity activity,
                                      Object imagePath,
                                      int roundCorners,
                                      RoundCornersTransformation.CornerType type,//圆角的位置
                                      ImageView imageView) {
        Glide.with(activity).load(imagePath)
                .apply(new RequestOptions().transforms(new CenterCrop(), new RoundCornersTransformation(activity, SizeUtils.dp2px(roundCorners), RoundCornersTransformation.CornerType.TOP)))
                .transition(new DrawableTransitionOptions().crossFade())
                .into(imageView);
    }

    /*加载图片*/
    public static void loadImage(Activity activity,
                                 Object imagePath,
                                 ImageView imageView) {
        Glide.with(activity).load(imagePath)
                .apply(new RequestOptions().centerCrop())
                .transition(new DrawableTransitionOptions().crossFade())
                .into(imageView);
    }

}
